//
//  DingJiuDianViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "DingJiuDianViewController.h"

@interface DingJiuDianViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;


@property (nonatomic, strong) NSMutableArray *citys;

@end

@implementation DingJiuDianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];
    [self setUpdata];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"zhekouTableViewCell" bundle:nil] forCellReuseIdentifier:@"zhekouCell2"];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"tableViewCell"];
    // Do any additional setup after loading the view.
}


- (void)addSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    self.tableView.bounces = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

#pragma mark --- tableView 代理方法 ---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        return 50;
    } else if (indexPath.row == 2){
        return 250;
    } else {
        return kScreenHeight - 300 - 114;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *identifier = @"jiudianCell";
        JiuDianViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[JiuDianViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
        }
        cell.imageV.userInteractionEnabled = YES;
        [cell.button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
        // cell.backgroundColor = [UIColor redColor];
        return cell;
    }else if (indexPath.row == 2) {
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"forIndexPath:indexPath];
        if (self.citys.count != 0) {
            NSInteger i = 0;
            for (UIImageView *image in cell.contentView.subviews) {
                if ([[image class] isSubclassOfClass:[UIImageView class]]) {
                    image.userInteractionEnabled = YES;
                    AllCountryModel *model = self.citys[i];
                    [image sd_setImageWithURL:[NSURL URLWithString:model.photo]];
                    i++;
                }
            }
        }
        return cell;
    }
    
    
    static NSString *identifier = @"zhekouCell2";
    zhekouTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.titleLabel.textAlignment = NSTextAlignmentCenter;
    cell.titleLabel.text = @"热门城市酒店酒店";
    cell.titleLabel.textColor = [UIColor blackColor];
    cell.label.hidden = YES;
    UIView *selecedView = [[UIView alloc] init];
    selecedView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = selecedView;
    return cell;
}



- (void)actionButton:(UIButton *)button
{
   // NSLog(@"123");
    CountrysViewController *countrysVC = [[CountrysViewController alloc] init];
    [self presentViewController:countrysVC animated:YES completion:nil];
}


- (NSMutableArray *)citys
{
    if (!_citys) {
        _citys = [NSMutableArray array];
    }
    return _citys;
}


// 解析数据
- (void)setUpdata
{
    [[TYDataManager shareManager] requestDataWithUrl:kLvTuAPI block:^ (NSData *data){
        
        NSMutableDictionary *diction = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = diction[@"data"];
        
        for (int i = 0; i < 9; i++) {
            NSDictionary *dic = arr[i];
            AllCountryModel *model = [[AllCountryModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.citys addObject:model];
        }

        [self.tableView reloadData];
        
    }];
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
